﻿using System;
using System.IO;
using System.Windows.Forms;
using Moonlite.ExtensionApi.GlobalInterfaces;
using Moonlite.Platform.General;
using Moonlite.Platform.Project;

namespace Moonlite.Compiling {
    public partial class CompilerOptionsForm : Form {
        private void ResetCompilerForm(bool prompt) {
            DialogResult dialogResult = prompt == true ? MessageBox.Show("Are you sure you want to reset?", "Moonlite Map Studio", 
                MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) : DialogResult.OK;

            if (prompt == true && dialogResult == DialogResult.No) {
                return;
            }

            textEditName.Text = "";
            textEditExecutablePath.Text = "";
            textEditDefaultCLIArgs.Text = "";
            checkEditAddPathToMap.Checked = true;
        }

        private void OnResetClick(object sender, EventArgs e) {
            ResetCompilerForm(true);
        }

        private void OnCancelClick(object sender, EventArgs e) {
            UpdateCompilers();
            ResetCompilerForm(false);
            Close();
        }

        private void OnOKClick(object sender, EventArgs e) {
            CompilerReader.Save(CompilerManager.Compilers);
            ResetCompilerForm(false);
            Close();
        }

        private void OnAddClick(object sender, EventArgs e) {
            if (ValidateControls() == true) {
                DefaultCompiler compiler = new DefaultCompiler(textEditName.Text, textEditExecutablePath.Text,
                    textEditDefaultCLIArgs.Text, checkEditAddPathToMap.Checked);

                CompilerManager.Compilers.Add(compiler);
                UpdateCompilers();
            }
        }

        private bool ValidateControls() {
            if (textEditName.Text == "") {
                ShowError("Invalid name entered.");
                return false;
            }

            if (textEditExecutablePath.Text == "") {
                ShowError("Invalid executable entered.");
                return false;
            }

            if (File.Exists(textEditExecutablePath.Text) == true) {
                return true;
            }

            ShowError("Entered executable doesn't exist.");
            return false;
        }

        private void OnRemoveClick(object sender, EventArgs e) {
            if (listBoxControlCompilers.SelectedIndex == -1) {
                return;
            }

            DialogResult dialogResult = MessageBox.Show("Are you sure you want to remove the selected compiler from the compiler list?",
                "Moonlite Map Studio", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);

            if (dialogResult == DialogResult.Yes) {
                ICompiler compiler = (ICompiler)listBoxControlCompilers.SelectedItem;
                CompilerManager.Compilers.Remove(compiler);
                UpdateCompilers();
            }
        }

        private void OnBrowseClick(object sender, EventArgs e) {
            if (openFileDialog.ShowDialog() == DialogResult.OK) {
                textEditExecutablePath.Text = openFileDialog.FileName;
            } 
        }

        private void OnMoveUpClick(object sender, EventArgs e) {
            object item = listBoxControlCompilers.SelectedItem;
            int index = listBoxControlCompilers.SelectedIndex;
            if (item != null && index > 0 && listBoxControlCompilers.Items.Count > 1) {
                index--;
                ICompiler compiler = (ICompiler)item;
                CompilerManager.Compilers.Remove(compiler);
                CompilerManager.Compilers.Insert(index, compiler);
                UpdateCompilers();
            }
        }

        private void OnMoveDownClick(object sender, EventArgs e) {
            object item = listBoxControlCompilers.SelectedItem;
            int index = listBoxControlCompilers.SelectedIndex;
            if (item != null && index < listBoxControlCompilers.Items.Count && listBoxControlCompilers.Items.Count > 1) {
                index++;
                ICompiler compiler = (ICompiler)item;
                CompilerManager.Compilers.Remove(compiler);
                CompilerManager.Compilers.Insert(index, compiler);
                UpdateCompilers();
            }
        }

        private void UpdateCompilers() {
            if (Global.Instance.Project == null) {
                return;
            }

            btnAdd.Enabled = true;
            btnRemove.Enabled = true;
            btnMoveDown.Enabled = true;
            btnMoveUp.Enabled = true;
            if (Global.Instance.Project.Type == ProjectType.Galaxy) {
                btnAdd.Enabled = false;
                btnRemove.Enabled = false;
                btnMoveDown.Enabled = false;
                btnMoveUp.Enabled = false;
            }

            listBoxControlCompilers.Items.Clear();
            foreach (ICompiler compiler in CompilerManager.Compilers) {
                if (Global.Instance.Project.Type == ProjectType.Jass) {
                    if (compiler.Type == CompilerType.Jass) {
                        listBoxControlCompilers.Items.Add(compiler);
                    }
                }
                else {
                    if (compiler.Type == CompilerType.Galaxy) {
                        listBoxControlCompilers.Items.Add(compiler);
                    }
                }
            }
        }

        public new DialogResult ShowDialog() {
            UpdateCompilers();
            return base.ShowDialog();
        }

        private static void ShowError(string message) {
            MessageBox.Show(message, "Moonlite Map Studio", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
        }

        public CompilerOptionsForm() {
            InitializeComponent();
            CompilerManager.LoadCompilers();
            UpdateCompilers();
        }
    }
}